package gui2.g4;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ChatGUI extends JFrame {

	private JLabel caption;

	private JLabel lblUsername;
	private JTextField fldUsername;
	private JButton btnStartChat;

	private JList chatList;
	private JScrollPane scroll;

	private JTable chatTable;

	private JTextField txtChat;
	private JButton btnSendText;

	private ChatModel chatModel = new ChatModel();

	public ChatGUI() {
		setTitle("Chat");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		createWidgets();
		layoutWidgets();
		setupInteraction();

		pack();
		setLocationRelativeTo(null);
	}

	private void setupInteraction() {
		btnSendText.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = txtChat.getText();
				chatModel.addMessage(new ChatMessage(text, fldUsername
						.getText()));

			}

		});

		txtChat.getDocument().addDocumentListener(new DocumentListener() {

			{
				updateSendButton();
			}

			private void updateSendButton() {
				String text = txtChat.getText();
				boolean enableButton = text != null && !text.trim().isEmpty();
				btnSendText.setEnabled(enableButton);
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				updateSendButton();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				updateSendButton();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				updateSendButton();
			}
		});

		getContentPane().addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent e) {
				Point mousePos = e.getPoint();
				if (mousePos.x <= 10
						&& mousePos.y >= getContentPane().getSize().height - 10) {
					String text = "Bin kurz AFK";
					chatModel.addMessage(new ChatMessage(text, fldUsername
							.getText()));
				}
			}
		});

	}

	private void createWidgets() {
		caption = new JLabel("CHAT");
		caption.setPreferredSize(new Dimension(500, 50));
		caption.setHorizontalAlignment(SwingConstants.CENTER);
		caption.setFont(caption.getFont().deriveFont(25f));

		lblUsername = new JLabel("Username:");
		fldUsername = new JTextField();
		fldUsername.setMaximumSize(new Dimension(220, 25));

		btnStartChat = new JButton("Start Chat!");

		ChatListModel listModel = new ChatListModel();
		chatModel.addListener(listModel);

		chatList = new JList(listModel);
		chatList.setCellRenderer(new ChatListCellRenderer());
		chatList.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		ChatTableModel tableModel = new ChatTableModel();
		chatModel.addListener(tableModel);

		chatTable = new JTable(tableModel);

		scroll = new JScrollPane(chatList);
		scroll.setPreferredSize(new Dimension(300, 300));

		txtChat = new JTextField();

		btnSendText = new JButton("Send");
	}

	private void layoutWidgets() {
		getContentPane().setLayout(new BorderLayout(5, 5));

		getContentPane().add(caption, BorderLayout.NORTH);

		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

		leftPanel.add(lblUsername);
		leftPanel.add(fldUsername);
		leftPanel.add(btnStartChat);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(2, 2, 2, 2);
		centerPanel.add(scroll, c);

		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(2, 2, 2, 2);
		centerPanel.add(chatTable, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		centerPanel.add(txtChat, c);

		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		centerPanel.add(btnSendText, c);

		getContentPane().add(leftPanel, BorderLayout.WEST);
		getContentPane().add(centerPanel, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				
				try {
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (UnsupportedLookAndFeelException e) {
					e.printStackTrace();
				}

				JFrame f = new ChatGUI();

				Character[] buchstaben = new Character[26];
				for (char c = 'a'; c <= 'z'; c++) {
					buchstaben[c - 97] = c;
				}

				Object o = JOptionPane.showInputDialog(f, buchstaben,
						buchstaben[0]);

				f.setVisible(true);

			}
		});
	}

}
